package web.demo.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import web.demo.dao.IFoodTableDao;
import web.demo.entity.FoodTable;
import web.demo.utils.JdbcUtils;

public class FoodTableDao implements IFoodTableDao{

	@Override
	public boolean addFoodTable(FoodTable foodTable) {
		String sql="INSERT INTO foodTable(NAME,STATUS) VALUES(?,?);";
		try{
			int result=JdbcUtils.getQueryRunner().update(sql,foodTable.getName(),foodTable.getStatus());
			if(result>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<FoodTable> getAll() {
		String sql="SELECT * FROM foodTable;";
		try{
			return JdbcUtils.getQueryRunner().query(sql,new BeanListHandler<FoodTable>(FoodTable.class));
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public List<FoodTable> getAll(int status) {
		String sql="SELECT * FROM foodTable where status=?";
		try{
			return JdbcUtils.getQueryRunner().query(sql,new BeanListHandler<FoodTable>(FoodTable.class),status);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public FoodTable getById(int id) {
		String sql="select * from foodTable where id=?";
		try {
			return JdbcUtils.getQueryRunner().query(sql,new BeanHandler<FoodTable>(FoodTable.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public void update(FoodTable foodTable) {
		String sql="UPDATE foodTable SET STATUS=?,prepare_time=? WHERE id=?;";
		try {
			JdbcUtils.getQueryRunner().update(sql,foodTable.getStatus(),foodTable.getPrepare_time(),foodTable.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean delete(int id) {
		//餐桌的删除必须在该餐桌没有被预定的情况下
		String sql="delete from foodTable where id=?";
		try {
			int result=JdbcUtils.getQueryRunner().update(sql,id);
			if(result>0){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	
	//通过餐桌名称查询，使用模糊查询，可以查询单个或多个餐桌
	@Override
	public List<FoodTable> select(String str) {
		String sql=null;
		Object condition=null;
		if(str.matches("[0-9]+")){
			//输入的是数字，按照id查询
			condition=Integer.parseInt(str);
			sql="select * from foodTable where id=?";
		}
		//匹配所有的中文，英文字符一次或多次，或者匹配所有的空格、制表符、换行符等空白字符0次或多次
		if(str.matches("[\u4e00-\u9fa5a-zA-Z]+|\\s*")){
			//输入的是中文，按照名称查询
			condition=str.toString();
			condition="%"+condition+"%";
			sql="select * from foodTable where name like ?";
		}
		try{
			return JdbcUtils.getQueryRunner().query(sql,new BeanListHandler<FoodTable>(FoodTable.class),condition);
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	
	
}
